package org.pfc.porlapatilla.model;

import java.util.Date;

import org.pfc.porlapatilla.DTO.MessageDTO;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * This class represents the message that an user send to other user.
 * 
 * @author jorgepolobermudez
 * 
 */
@Document
public class Message {

    /**
     * The message id.
     */
    @Id
    private String id;

    /**
     * The message text.
     */
    private String text;

    /**
     * The message sender.
     */
    @DBRef
    private User sender;

    /**
     * The message receiver.
     */
    @DBRef
    private User receiver;

    /**
     * The message date.
     */
    private Date date;

    /**
     * If the message have been readed.
     */
    private boolean readed;

    /**
     * Default constructor
     */
    public Message() {
    }

    /**
     * Creates a new message.
     * 
     * @param text
     *            The new message text.
     * @param sender
     *            The new message sender.
     * @param receiver
     *            The new message receiver.
     * @param date
     *            The new message date.
     */
    public Message(String text, User sender, User receiver, Date date) {
	this.text = text;
	this.sender = sender;
	this.receiver = receiver;
	this.date = date;
	this.readed = false;
    }

    /**
     * Get the message id.
     * 
     * @return The message id.
     */
    public String getId() {
	return id;
    }

    /**
     * Set the message id.
     * 
     * @param id
     *            The id to set.
     */
    public void setId(String id) {
	this.id = id;
    }

    /**
     * Get the message text.
     * 
     * @return The message text.
     */
    public String getText() {
	return text;
    }

    /**
     * Set the message text.
     * 
     * @param text
     *            The text to set.
     */
    public void setText(String text) {
	this.text = text;
    }

    /**
     * Get the message sender.
     * 
     * @return The message sender.
     */
    public User getSender() {
	return sender;
    }

    /**
     * Set the message sender.
     * 
     * @param sender
     *            The sender to set.
     */
    public void setSender(User sender) {
	this.sender = sender;
    }

    /**
     * Get the message receiver.
     * 
     * @return The message receiver.
     */
    public User getReceiver() {
	return receiver;
    }

    /**
     * Set the message receiver.
     * 
     * @param receiver
     *            The receiver to set.
     */
    public void setReceiver(User receiver) {
	this.receiver = receiver;
    }

    /**
     * Get the message date.
     * 
     * @return the date
     */
    public Date getDate() {
	return date;
    }

    /**
     * Set the message date.
     * 
     * @param date
     *            the date to set
     */
    public void setDate(Date date) {
	this.date = date;
    }

    /**
     * If the message has been readed.
     * 
     * @return the readed
     */
    public boolean isReaded() {
	return readed;
    }

    /**
     * Set the message readed.
     * 
     * @param readed
     *            the readed to set
     */
    public void setReaded(boolean readed) {
	this.readed = readed;
    }

    /**
     * Creates a new messageDTO from a message.
     * 
     * @return A new messageDTO.
     */
    public MessageDTO toDTO() {
	return new MessageDTO(this.id, this.text, this.sender.getNick(),
		this.receiver.getNick(), this.date, this.readed);
    }

}
